package com.qf.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.MdUnitDTO;
import com.qf.entity.MdUnitMeasure;
import com.qf.service.MdUnitMeasureService;
import com.qf.util.R;
import lombok.extern.log4j.Log4j2;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
 * ==== 勤能补拙 ====
 * @author dzz
 * @date 2025/4/22
 * <p>
 * ==== 行稳致远 进而有为 ====
 */
@Log4j2
@RestController
@RequestMapping("/md")
public class MdUnitMeasureController {
    private Logger logger = LogManager.getLogger(MdUnitMeasureController.class);
    @Autowired
    private MdUnitMeasureService mdUnitMeasureService;
    /**
     * 查询所有单位计量信息
     * @param mdUnitDTO
     * @return
     */
    @GetMapping("/unit/measure/list")
    public R getUnitMeasureList(MdUnitDTO mdUnitDTO) {
        PageHelper.startPage(mdUnitDTO.getPageNum(), mdUnitDTO.getPageSize());
        List<MdUnitMeasure> list = mdUnitMeasureService.getUnitMeasureList(mdUnitDTO);
        PageInfo<MdUnitMeasure> pageInfo = new PageInfo(list);
        logger.info("查询到数据：" + list);
        return R.ok(pageInfo);
    }

    /**
     * 添加单位计量信息
     * @param mdUnitMeasure
     * @return
     */
    @PostMapping("unit/measure/add")
    public R addUnitMeasure(@RequestBody MdUnitMeasure mdUnitMeasure) {
        int i = mdUnitMeasureService.addUnitMeasure(mdUnitMeasure);
        if (ObjectUtils.isEmpty(i)) {
            return R.fail("添加失败");
        }
        return R.ok("添加成功受影响行数为:"+i);
    }
    /****
     * 修改单位计量信息
     * @param mdUnitMeasure
     * @return
     */
    @PostMapping("/unit/measure/update")
    public R updateUnitMeasure(@RequestBody MdUnitMeasure mdUnitMeasure) {
        int i = mdUnitMeasureService.updateUnitMeasure(mdUnitMeasure);
        if (ObjectUtils.isEmpty(i)) {
            return R.fail("修改失败");
        }
        return R.ok("修改成功受影响行数:"+i);
    }
    /**
     * 删除单位计量信息
     * @param
     * @return
     */
    @GetMapping("/unit/measure/deleteById/{id}")
    public R delUnitMeasure(@PathVariable("id") int id) {
        int i = mdUnitMeasureService.delUnitMeasure(id);
        if (ObjectUtils.isEmpty(i)) {
            return R.fail("删除失败");
        }
        return R.ok(i);
    }
    /**
     * 批量删除单位计量信息
     */
    @PostMapping("/unit/measure/delBatch")
    public R delUnitMeasureBatch(@RequestBody List<Integer> ids) {
        mdUnitMeasureService.delUnitMeasureBatch(ids);
       return R.ok("删除成功");
    }

}
